package com.h3c.nem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eos.common.util.IdWorker;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.h3c.nem.entity.SdEnumCode;
import com.h3c.nem.mapper.SdEnumCodeMapper;
import com.h3c.nem.service.ISdEnumCodeService;
import com.h3c.nem.dto.SdEnumCodeDTO;
import com.h3c.nem.utils.InitExampleOrderByUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * Copyright (C), 2020-2020, h3c
 * FileName: SdEnumCodeServiceImpl
 *
 * @Author: l22886
 * @Date: 2022-01-19
 * Description: sd_enum_code serviceImpl
 * Version: 1.0
 */
@Slf4j
@Service
public class SdEnumCodeServiceImpl extends ServiceImpl<SdEnumCodeMapper, SdEnumCode> implements ISdEnumCodeService {

    @Resource
    private SdEnumCodeMapper sdEnumCodeMapper;



    /**
     * @title: selectFuzzyByDto
     * @description: 模糊查询操作
     * @params: [ dto ]
     * @return: java.util.List
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public List<SdEnumCodeDTO> selectFuzzyByDto(SdEnumCodeDTO dto) {
        //List<SdEnumCode> list = sdEnumCodeMapper.selectByExample(initFuzzyExample(dto));
        List<SdEnumCode> list = sdEnumCodeMapper.queryListByFuzzy(JSONObject.parseObject(JSONObject.toJSONString(dto),SdEnumCode.class));
        return JSONArray.parseArray(JSONArray.toJSONString(list), SdEnumCodeDTO.class);
    }

    /**
     * @title: selectByDto
     * @description: 精确查询操作
     * @params: [ dto ]
     * @return: java.util.List
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public List<SdEnumCodeDTO> selectByDto(SdEnumCodeDTO dto) {
        //List<SdEnumCode> list = sdEnumCodeMapper.selectByExample(initExample(dto));
        List<SdEnumCode> list = sdEnumCodeMapper.queryList(JSONObject.parseObject(JSONObject.toJSONString(dto),SdEnumCode.class));
        return JSONArray.parseArray(JSONArray.toJSONString(list), SdEnumCodeDTO.class);
    }

    @Override
    public List<SdEnumCodeDTO> getEnumByCode(SdEnumCodeDTO dto) {
        QueryWrapper<SdEnumCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SdEnumCode::getEnumCode, dto.getEnumCode());
        List<SdEnumCode> sdEnumCodes = sdEnumCodeMapper.selectList(queryWrapper);
        return JSONArray.parseArray(JSONArray.toJSONString(sdEnumCodes), SdEnumCodeDTO.class);
    }

    /**
     * @title: insertDto
     * @description: 插入操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer insertDto(SdEnumCodeDTO dto) {
        //if (null == dto.getId() || "".equals(dto.getId())) {
        //    String pkId = IdWorker.nextId() + "";
        //    dto.setId(pkId);
        //}
        return sdEnumCodeMapper.insertSdEnumCode(JSONObject.parseObject(JSON.toJSONString(dto), SdEnumCode.class));
    }

    /**
     * @title: updateDto
     * @description: 修改操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer updateDto(SdEnumCodeDTO dto) {
        //if (null != dto && null != dto.getId() && !"".equals(dto.getId())) {
            return sdEnumCodeMapper.updateSdEnumCode(JSONObject.parseObject(JSON.toJSONString(dto), SdEnumCode.class));
            //return sdEnumCodeMapper.updateByPrimaryKey(JSONObject.parseObject(JSON.toJSONString(dto), SdEnumCode.class));
        //} else {
        //    return 0;
        //}
    }

    /**
     * @title: deleteDto
     * @description: 删除操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer deleteDto(SdEnumCodeDTO dto) {
        //if (null != dto && null != dto.getId() && !"".equals(dto.getId())) {
            return sdEnumCodeMapper.deleteSdEnumCode(JSONObject.parseObject(JSON.toJSONString(dto), SdEnumCode.class));
            //return sdEnumCodeMapper.deleteByPrimaryKey(dto.getId());
        //} else {
        //    return 0;
        //}
    }


    /**
     * @title: initFuzzyExample
     * @description: 初始化 模糊查询 example
     * @params: [ dto ]
     * @return: tk.mybatis.mapper.entity.Example
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    private Example initFuzzyExample(SdEnumCodeDTO dto) {
        Example example = InitExampleOrderByUtils.initExampleOrderBy(SdEnumCode.class, dto.getOrderBy());
        Example.Criteria criteriaName = example.createCriteria();
        if (null != dto) {
            if (null != dto.getEnumCode() && !"".equals(dto.getEnumCode())) {
                criteriaName.andLike("EnumCode", "%" + dto.getEnumCode() + "%");
            }
            if (null != dto.getEnumName() && !"".equals(dto.getEnumName())) {
                criteriaName.andLike("EnumName", "%" + dto.getEnumName() + "%");
            }
            if (null != dto.getValue() && !"".equals(dto.getValue())) {
                criteriaName.andLike("Value", "%" + dto.getValue() + "%");
            }
            if (null != dto.getNameZh() && !"".equals(dto.getNameZh())) {
                criteriaName.andLike("NameZh", "%" + dto.getNameZh() + "%");
            }
            if (null != dto.getNameEn() && !"".equals(dto.getNameEn())) {
                criteriaName.andLike("NameEn", "%" + dto.getNameEn() + "%");
            }
            if (null != dto.getEnumRemark() && !"".equals(dto.getEnumRemark())) {
                criteriaName.andLike("EnumRemark", "%" + dto.getEnumRemark() + "%");
            }
            if (null != dto.getSortId() && !"".equals(dto.getSortId())) {
                criteriaName.andLike("SortId", "%" + dto.getSortId() + "%");
            }
        }
        return example;
    }

    /**
     * @title: initExample
     * @description: 初始化 精确查询 example
     * @params: [ dto ]
     * @return: tk.mybatis.mapper.entity.Example
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    private Example initExample(SdEnumCodeDTO dto) {
        Example example = InitExampleOrderByUtils.initExampleOrderBy(SdEnumCode.class, dto.getOrderBy());
        Example.Criteria criteriaName = example.createCriteria();
        if (null != dto) {
            if (null != dto.getEnumCode() && !"".equals(dto.getEnumCode())) {
                criteriaName.andEqualTo("EnumCode", dto.getEnumCode());
            }
            if (null != dto.getEnumName() && !"".equals(dto.getEnumName())) {
                criteriaName.andEqualTo("EnumName", dto.getEnumName());
            }
            if (null != dto.getValue() && !"".equals(dto.getValue())) {
                criteriaName.andEqualTo("Value", dto.getValue());
            }
            if (null != dto.getNameZh() && !"".equals(dto.getNameZh())) {
                criteriaName.andEqualTo("NameZh", dto.getNameZh());
            }
            if (null != dto.getNameEn() && !"".equals(dto.getNameEn())) {
                criteriaName.andEqualTo("NameEn", dto.getNameEn());
            }
            if (null != dto.getEnumRemark() && !"".equals(dto.getEnumRemark())) {
                criteriaName.andEqualTo("EnumRemark", dto.getEnumRemark());
            }
            if (null != dto.getSortId() && !"".equals(dto.getSortId())) {
                criteriaName.andEqualTo("SortId", dto.getSortId());
            }
        }
        return example;
    }


}
